#include "BuddyTreeItem.h"
#include "../StdUserInterfaceModule/Icons.h"
#include "../../core/Impp.h"

namespace Impp {


BuddyTreeItem::BuddyTreeItem(Buddy* buddy) :
	buddy(buddy)
{
}

BuddyTreeItem::~BuddyTreeItem()
{
}

void BuddyTreeItem::paint(wxGCDC & dc, const wxPoint& pos, const wxSize& size, bool showAva, bool dimIdle){
	iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
	wxASSERT(ui != NULL);
	wxBitmap statusIcon = ui->getIcons()->getStatusIcon(buddy->getStatus());

	bool hasUnreadMsgs = buddy->hasUnredMsg();
	bool hasNoAuth = !buddy->hasAuthorisation();

	Status status = Impp::exemplar()->getSelf(buddy->getAccountId())->getStatus();

	bool visibilityDifferent = (status == INVISIBLE && buddy->isOnPrivacyList(BUDDY_PRIVACY_VISIBLELIST)) ||
							   (status != INVISIBLE && buddy->isOnPrivacyList(BUDDY_PRIVACY_INVISIBLELIST));

	wxDateTime idleTime;
	wxTimeSpan idleTimeSpan;
	wxString idleTimeString;
	bool isIdle = buddy->getIdleTime(idleTime);
	wxChar alpha = 255;

	if(isIdle){
		idleTimeSpan = wxDateTime::Now()-idleTime;

		if(idleTimeSpan.GetHours() != 0 ){
			idleTimeString = wxString::Format(wxT(" - idle %ih %imin"), idleTimeSpan.GetHours(),idleTimeSpan.GetMinutes()-60*idleTimeSpan.GetHours());
		}else{
			idleTimeString = wxString::Format(wxT(" - idle %imin"), idleTimeSpan.GetMinutes());
		}

		if(dimIdle){
			alpha = 150;
			wxColour cur = dc.GetTextForeground();
			dc.SetTextForeground(wxColor(cur.Red(),cur.Blue(), cur.Green(), alpha));
			cur = dc.GetBrush().GetColour();
			dc.SetBrush(wxColor(cur.Red(),cur.Blue(), cur.Green(), 255-alpha));
		}
	}

	if(showAva){
		wxBitmap ava = buddy->getAvatar();
		if(ava.IsNull()){
			iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
			wxASSERT(ui != NULL);
			ava = ui->getIcons()->getSpecialBitmap(BITMAP_NO_AVATAR);
		}

		//wxBitmap avaScaled = ava->Rescale(0,0,ava->GetWidth(), ava->GetHeight(),28,28);
		wxImage image = ava.ConvertToImage();
		wxBitmap avaScaled = wxBitmap(image.Scale(28,28,wxIMAGE_QUALITY_HIGH));


		dc.DrawBitmap(avaScaled,pos+wxPoint(2,3),false);
		dc.DrawBitmap(statusIcon,pos+wxPoint(16,16),true);

		if(isIdle && dimIdle){
			dc.DrawRectangle(pos+wxPoint(1,1),wxSize(32,32));
		}


		wxFont font = dc.GetFont();
		if(visibilityDifferent){
			font.SetStyle(wxFONTSTYLE_ITALIC);
			dc.SetFont(font);
		}

		dc.DrawText(buddy->getName(), pos+wxPoint(34,1));

		font.SetStyle(wxFONTSTYLE_NORMAL);
		dc.SetFont(font);
		dc.SetTextForeground(wxColor(80,80,80,alpha));

		wxString statusMsg;
		if(!hasNoAuth){
			statusMsg = buddy->getStatusMsg();

			if(statusMsg.IsEmpty()){
				statusMsg = statusToString(buddy->getStatus());
				if(isIdle){
					statusMsg << idleTimeString;
				}
			}else{
				//replace new lines
				statusMsg.Replace(wxT("\n"),wxT(" "), true);
				statusMsg.Replace(wxT("\r"),wxT(" "), true);
				if(isIdle){
					statusMsg = statusToString(buddy->getStatus()) << idleTimeString << wxT(": ") << statusMsg;
				}else{
					statusMsg = statusToString(buddy->getStatus()) <<  wxT(": ") << statusMsg;
				}

			}
		}else{
			statusMsg = wxT("NO ATUHORIZATION");
		}
		dc.DrawText(statusMsg, pos+wxPoint(34,17));

		if(hasUnreadMsgs){
			iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
			wxASSERT(ui != NULL);
			dc.DrawBitmap(ui->getIcons()->getSpecialBitmap(BITMAP_NEW_MSG),pos + wxPoint(size.x-20,1),true);
		}

	}else{
		dc.DrawBitmap(statusIcon,pos+wxPoint(0,1),true);
		if(isIdle && dimIdle){
			dc.DrawRectangle(pos+wxPoint(0,1),wxSize(16,16));
		}
		wxFont font = dc.GetFont();
		if(visibilityDifferent){
			font.SetStyle(wxFONTSTYLE_ITALIC);
			dc.SetFont(font);
		}
		dc.DrawText(buddy->getName(), pos+wxPoint(18,1));
		font.SetStyle(wxFONTSTYLE_NORMAL);
		dc.SetFont(font);
		if(hasUnreadMsgs){
			iUserInterface* ui = Impp::exemplar()->getModuleManager()->getUserInterfaceModule();
			wxASSERT(ui != NULL);
			dc.DrawBitmap(ui->getIcons()->getSpecialBitmap(BITMAP_NEW_MSG),pos + wxPoint(size.x-20,1),true);
		}
	}





}

Buddy* BuddyTreeItem::getBuddy(){
	return buddy;
}

}
